/**
 * fileName: MessageTag.java
 *     Date: 2012-4-29
 **/
package com.stranger.common.tag;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @作者 stranger.lin
 *
 * @时间 2012-4-29
 */
public class PageLoadingTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7518648646792011369L;
	
	private String id;
	private String title;
	private String content;
	private 	JspWriter out;
	
	/**
	 * 	<div id="dialog-message" title="Download complete">
	 *	<p>
	 *		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
	 *		<span> Your files have downloaded successfully into the My Downloads folder.<span>
	 *	</p>
	 *	<p>
	 *		Currently using <b>36% of your storage space</b>.
	 *	</p>
	 *  $( "#dialog-message" ).dialog({
			modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			}
		});
	 *	</div>
	 * */
	@Override
	public int doStartTag() throws JspException {
		try {
			out=pageContext.getOut();
			if (id==null||"".equals(id)) {
				id="_message_"+UUID.randomUUID();
			}
			if (title==null||"".equals(title)) {
				title="提示";
			}
			
			StringBuffer html=new StringBuffer();
			html.append("<div id=\"UIloading\" style=\"z-index: 3000;\" class=\"ui-widget-overlay\"> <div style=\"text-align: center;margin-top: 20%;\"><img alt=\"loading...\" height=\"20\" border=\"0\" src=\"images/common/barLoading.gif\"></div></div>");
			html.append("<div id=\""+id+"\" title=\""+title+"\"  style=\"display:none;\">");
			html.append("<p id=\"msgHTML\">");
			//html.append("<span class=\"ui-icon ui-icon-circle-check\" style=\"float:left; margin:0 7px 50px 0;\"></span>");
			html.append("<font style=\"text-align: center;font-size: 15px;\" id=\"_msgContent_\"></font>");
			html.append("</p>");
			html.append("</div>");
			html.append("<script>");
			html.append("function showMsg(content){");
			html.append("$(\"#_msgContent_\").html(content);");
			html.append("$(\"#"+id+"\" ).dialog({\n");
			html.append("modal: true,\nbuttons: {\n");
			html.append("确定: function() {$( this ).dialog( \"close\" );}\n");
			html.append("}\n});");
			html.append("}");
			html.append("</script>");
			out.print(html);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		if (out!=null) {
			try {
				StringBuffer html=new StringBuffer();
				html.append("<script>$(function(){\n");
				html.append("$(\"#UIloading\").hide();\n");
				html.append("})</script>");
				out.print(html);
				out.flush();
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		return EVAL_PAGE;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	
	

}
